package com.caimiao.miao.page.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.caimiao.miao.MiaoApplication;
import com.caimiao.miao.R;
import com.caimiao.miao.interfaces.BaseActivity;
import com.caimiao.miao.library.push.PushControl;
import com.caimiao.miao.library.utils.PixelUtil;
import com.caimiao.miao.library.utils.tip.LogUtil;
import com.caimiao.miao.library.utils.tip.PrintLog;
import com.caimiao.miao.library.utils.tip.ToastUtil;
import com.caimiao.miao.page.master.MasterActivity;
import com.caimiao.miao.service.MiaoService;

public class HomeActivity extends BaseActivity<IHomeContact.Presenter> implements IHomeContact.View,
        ViewPager.OnPageChangeListener, View.OnClickListener, View.OnTouchListener {
    private ViewPager mViewPager;
    private ImageView mIvQrCode;
    private ImageButton mBtnShow;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_advert;
    }

    @Override
    protected void initUI() {
        mViewPager = findViewById(R.id.advertVp);
        mIvQrCode = findViewById(R.id.advertIvQrCode);
        mBtnShow = findViewById(R.id.advertBtnShow);
    }

    @Override
    protected void initLogic() {
        findViewById(R.id.advertIBtnTest).setOnClickListener(this::onClick);
        mBtnShow.setOnClickListener(this::onClick);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setOnTouchListener(this::onTouch);
        new HomePresenter(this);
        if (!MiaoService.isStart()) {
            startService(new Intent(this, MiaoService.class));
        }
        mPresenter.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PrintLog.close();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void setAdapter(PagerAdapter adapter) {
        mViewPager.setAdapter(adapter);
    }

    @Override
    public void setCurrentItem(int index) {
        mViewPager.setCurrentItem(index, true);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mPresenter.setCurrentItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == ViewPager.SCROLL_STATE_IDLE) {
            int curIndex = mViewPager.getCurrentItem();
            int lastIndex = mViewPager.getAdapter().getCount() - 2;
            ///无动画切换时mViewPager.setCurrentItem(index, false)，设置FLag
            if (curIndex == 0) {
                mViewPager.setCurrentItem(lastIndex, false);
            } else if (curIndex > lastIndex) {
                mViewPager.setCurrentItem(1, false);
            }
            mPresenter.start();
        } else {
            mPresenter.stop();
        }
    }

    @Override
    public void setPresenter(IHomeContact.Presenter presenter) {
        super.setPresenter(presenter);
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mIvQrCode.getLayoutParams();
        presenter.setLogic(this, params.width, params.height);
    }

    @Override
    public void setQRCode(Bitmap qrCode) {
        mIvQrCode.setImageBitmap(qrCode);
    }

    @Override
    public void setHideQrCode() {
        mBtnShow.setVisibility(View.VISIBLE);
        mIvQrCode.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.advertBtnShow:
                PushControl.getInstance().setAlias();
                v.setVisibility(View.GONE);
                mIvQrCode.setVisibility(View.VISIBLE);
                mPresenter.startTimingShow();
                break;
            case R.id.advertIBtnTest:
                //test
                startActivity(new Intent(this, MasterActivity.class));
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        return false;
    }
}
